package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.ICardEntityService;
import com.younion.business.IFavoritesService;
import com.younion.business.IMemberCardService;
import com.younion.common.constants.SysConstants;
import com.younion.model.CardDetailedResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.API_CardEntityVO;
import com.younion.vo.API_MemberCardVO;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.CardEntityVO;
import com.younion.vo.FavoritesVO;

public class CardDetailGetConsumer extends AbstractRequestConsumer {
	private ICardEntityService cardEntityService;
	private IMemberCardService memberCardService;
	private IFavoritesService favoritesService;

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}

	public void setFavoritesService(IFavoritesService favoritesService) {
		this.favoritesService = favoritesService;
	}

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getCardDetail(request, response);
	}

	public String getCardDetail(HttpServletRequest request,
			HttpServletResponse response) {
		String card_id = request.getParameter("card_id");
		CardDetailedResult cardDetailedResult = new CardDetailedResult();
		String user_token = request.getParameter("user_token");
		List<API_MemberCardVO> api_MemberCardVOList = null;
		List<FavoritesVO> favoritesVOList = null;
		ResultObject resultObject = new ResultObject();
		try {
			Map paramMap = new HashMap();
			paramMap.put("id", card_id);
			API_CardEntityVO cardEntityVO = cardEntityService
					.selectApiCardEntityByMap(paramMap);
			if (cardEntityVO != null) {
				// 会员登录情况下获取会员卡需要查询会员所拥有的卡
				if (StringUtils.isNotEmpty(user_token)) {// 登录的情况下
					api_MemberCardVOList = (List<API_MemberCardVO>) memberCardService
							.getMyCardByUserToken(user_token);
					Map favoritesParamMap = new HashMap();
					favoritesParamMap.put("userToken", user_token);
					favoritesVOList = (List<FavoritesVO>) favoritesService
							.getByMap(favoritesParamMap);
				}
				List<CardEntityVO> list = (List) cardEntityService
						.getByMap(paramMap);
				String img_url_pre = ConfigHelper.fastGetConfig("interface",
						"img_url_pre", "http://localhost:8080/younonmember/");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				cardDetailedResult.setCard_directions(cardEntityVO
						.getUseNotes());
				cardDetailedResult.setCard_id(card_id);
				cardDetailedResult.setCard_name(cardEntityVO.getCardName());
				cardDetailedResult.setTime_from(sdf.format(cardEntityVO
						.getExpiryDateFrom()));
				cardDetailedResult.setTime_to(sdf.format(cardEntityVO
						.getExpiryDateTo()));
				cardDetailedResult.setCard_num(cardEntityVO
						.getCardIssueAmount().toString());
				int limitAmount = cardEntityVO.getLimitAmount() == null ? 0
						: cardEntityVO.getLimitAmount();
				int issueAmount = cardEntityVO.getCardIssueAmount() == null ? 0
						: cardEntityVO.getCardIssueAmount();
				cardDetailedResult.setCard_surplus_num(new Integer(limitAmount
						- issueAmount).toString());
				cardDetailedResult.setInput_fileds(cardEntityVO
						.getInputFileds());
				cardDetailedResult.setNonactivatedPic(img_url_pre
						+ SysConstants.CARD_IMG_URL + "/"
						+ cardEntityVO.getId() + "/"
						+ cardEntityVO.getNonactivatedPic());
				cardDetailedResult.setActivatedPic(img_url_pre
						+ SysConstants.CARD_IMG_URL + "/"
						+ cardEntityVO.getId() + "/"
						+ cardEntityVO.getActivatedPic());
				cardDetailedResult.setCluber_welfare(cardEntityVO.getWelfare());
				cardDetailedResult
						.setAgioInfo(cardEntityVO.getAgioInfo() == null ? ""
								: cardEntityVO.getAgioInfo());
				cardDetailedResult.setCategory_id(cardEntityVO.getCustomerVO()
						.getCustomerKindId().toString());
				cardDetailedResult.setIs_follow("0");
				cardDetailedResult.setIs_league(cardEntityVO.getCustomerVO()
						.getHaveAllianceFavourable().toString());
				cardDetailedResult.setIs_store_club(cardEntityVO
						.getCustomerVO().getIsCoreCustomer().toString());
				cardDetailedResult.setShop(cardEntityVO.getCustomerVO()
						.getShops());
				cardDetailedResult.setNearby("1000");// TODO 需要配置附近的距离范围
				cardDetailedResult.setIs_new("1");// TODO 需要算出时间最新的
				cardDetailedResult
						.setIs_hot(cardEntityVO.getIsHot().toString());
				cardDetailedResult.setIs_recommend(cardEntityVO
						.getIsRecommend().toString());
				String user_card = "";
				if (api_MemberCardVOList != null
						&& api_MemberCardVOList.size() > 0) {
					for (API_MemberCardVO api_MemberCardVO : api_MemberCardVOList) {
						if (api_MemberCardVO.getApi_CardEntityVO().getId()
								.equals(cardEntityVO.getId())) {
							user_card = api_MemberCardVO.getId().toString();
						}
					}
				}
				cardDetailedResult.setUser_card(user_card);
				if (favoritesVOList != null) {
					for (FavoritesVO favoritesVO : favoritesVOList) {
						if (favoritesVO.getFavourId().equals(cardEntityVO.getId())) {
							cardDetailedResult.setIs_follow("1");
						}
					}
				}
				cardDetailedResult.setHave_favourable(cardEntityVO
						.getCustomerVO().getHaveFavourable().toString());
			}

			resultObject.setResult(cardDetailedResult);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}

		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setCardEntityService(ICardEntityService cardEntityService) {
		this.cardEntityService = cardEntityService;
	}
}
